package cc.fs.zhihuapp.utils.myutils;

import android.util.Log;
import android.view.SurfaceHolder;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import cc.fs.zhihuapp.utils.MediaPlayerProxy;
import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

/**
 * Created by fostion on 2015/10/30.
 * 封装：player
 *
 */
public class MyPlayer implements IMediaPlayer.OnCompletionListener,IMediaPlayer.OnPreparedListener,IMediaPlayer.OnBufferingUpdateListener{

    private IjkMediaPlayer ijkMediaPlayer;
    private PlayerListener playerListener;
    private MyMediaPlayerProxy proxy;
    private Timer timer = new Timer();

    public boolean isUpdate = true;
    boolean useProxy = true;

    public MyPlayer(){
        ijkMediaPlayer = new IjkMediaPlayer();
        ijkMediaPlayer.setOnCompletionListener(this);
        ijkMediaPlayer.setOnPreparedListener(this);
        ijkMediaPlayer.setOnBufferingUpdateListener(this);
        ijkMediaPlayer.setOnInfoListener(new IMediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(IMediaPlayer mp, int what, int extra) {
                switch (what){
                    case IMediaPlayer.MEDIA_INFO_BUFFERING_START:
                        if(playerListener != null){
                            playerListener.onBuffering();
                        }
                        break;

                    case IMediaPlayer.MEDIA_INFO_BUFFERING_END:
                        if(playerListener != null){
                            playerListener.onPrepared();
                        }
                        break;

                }
                return false;
            }
        });
    }

    public void playUrl(String url){
        //todo 开启代理
//        if (useProxy) {
//            startProxy();
//            url = proxy.getProxyURL(url);
//        }

        try {
            ijkMediaPlayer.setDataSource(url);
            ijkMediaPlayer.setLooping(false);
            ijkMediaPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void setSurcefaceHolder(SurfaceHolder _holder){
        ijkMediaPlayer.setDisplay(_holder);
    }

    public boolean isPlaying(){
        return ijkMediaPlayer.isPlaying();
    }

    public void start(){
        isUpdate = true;
        ijkMediaPlayer.start();
    }

    public void pause(){
        isUpdate = false;
        ijkMediaPlayer.pause();
    }

    public void setUpdate(boolean _isUpdate){
        isUpdate = _isUpdate;
    }

    public void stop(){
        isUpdate = false;
        ijkMediaPlayer.stop();
    }

    public void relase(){
        if(ijkMediaPlayer.isPlaying())
            ijkMediaPlayer.stop();
//        proxy.stop();
        timer.cancel();
        ijkMediaPlayer.release();
    }

    public void setUseProxy(boolean useProxy) {
        this.useProxy = useProxy;
    }

    public void setPlayerListener(PlayerListener _PlayerListener){
        this.playerListener = _PlayerListener;
    }

    public PlayerListener getPlayerListener() {
        return playerListener;
    }

    @Override
    public void onBufferingUpdate(IMediaPlayer mp, int percent) {
        if(playerListener != null)
            playerListener.onBuffered(percent);
    }

    @Override
    public void onCompletion(IMediaPlayer mp) {
        if(ijkMediaPlayer != null)
            playerListener.onCompletion();
    }

    @Override
    public void onPrepared(IMediaPlayer mp) {
        if(playerListener != null)
            playerListener.onPrepared();

        mp.start();
        timer.schedule(mTimerTask,0,500);
    }

    public void seekTo(int position){
        int cTime = position;
        int nTime =  ((Number) ijkMediaPlayer.getCurrentPosition()).intValue();
        long offsetTime = ijkMediaPlayer.getCurrentPosition() + cTime - nTime;
        ijkMediaPlayer.seekTo(offsetTime);
    }

    public void seekBack(){
        if(isPlaying()){
            long position = ijkMediaPlayer.getCurrentPosition() - 20000;
            ijkMediaPlayer.seekTo(position);
        }
    }

    public void seekForward(){
        if(isPlaying()){
            long position = ijkMediaPlayer.getCurrentPosition() + 20000;
            ijkMediaPlayer.seekTo(position);
        }
    }

    /*******************************************************
     * 通过定时器获取时间
     ******************************************************/
    TimerTask mTimerTask = new TimerTask() {
        @Override
        public void run() {
            if (ijkMediaPlayer == null || !isUpdate)
                return;
            if (ijkMediaPlayer.isPlaying()) {
                int position = ((Number)ijkMediaPlayer.getCurrentPosition()).intValue();
                int duration =((Number) ijkMediaPlayer.getDuration()).intValue();

                if(playerListener != null) {
                    playerListener.onProgress(duration, position);
                }
            } else {
                if(playerListener != null){
                    playerListener.onBuffering();
                }
            }
        }
    };

    private void startProxy() {
        if (proxy == null) {
            proxy = new MyMediaPlayerProxy();
            proxy.init();
            proxy.start();
        }
    }

    public interface PlayerListener{
        void onPrepared();
        void onBuffering();
        void onBuffered(int buffered);
        void onProgress(int total, int progress);
        void onCompletion();
    }

}
